package com.longrise.common.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.longrise.common.baseapp.BaseAppLike;



/**
 * Created by callxkj on 2017/2/27.
 * 跟系统手机相关的工具类
 */

public class AppUtil {

    /**
     * @return 全局application context
     */
    public static Context getContext(){
        return BaseAppLike.getAPP().getApplicationContext();
    }

    /**
     * 获取本地资源文件
     */
    public static Resources getResources(){
        return getContext().getResources();
    }

    /**
     * 获取配置的String
     * @param resId 资源id
     * @return  String
     */
    public static String getString(int resId){
        return getResources().getString(resId);
    }

    /**
     * 获取配置的color
     * @param resId 资源id
     * @return color
     */
    public static int getColor(int resId){
        return getResources().getColor(resId);
    }


    /**
     * @return app的版本号
     */
    public static int getAppVersionCode(){
        int versionCode = 0;
        try {
            PackageManager pm = getContext().getPackageManager();
            if (null != pm) {
                PackageInfo packageInfo = pm.getPackageInfo( getContext().getPackageName(), 0);
                if (null != packageInfo) {
                    versionCode = packageInfo.versionCode;
                }
            }
        } catch (Exception e) {

        }
        return versionCode;
    }


    /**
     * 屏幕宽度
     */
    public static int getScreenWidth() {
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    /**
     * 屏幕高度
     */
    public static int getScreenHeight() {
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(float pxValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 根据手机的分辨率from dp 的单位 转成为 px(像素)
     */
    public static int dip2px(float dpValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**将px值转换为sp值，保证文字大小不变*/
    public static int px2sp(float pxValue) {
        final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    public static int getStatusBar(Context cxt) {
        int statusBarHeight = -1;
        int resourceId = cxt.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }

}
